<?php
/*由bean脚本生成 powerBy--joe*/

namespace app\common\manage;

use app\common\bean\UserBean;
use app\common\enum\UserImageTypeEnum;
use app\common\validate\UserImageValidate;
use app\common\model\UserImageModel;
use app\common\bean\UserImageBean;
use app\common\bean\ListMap;
use app\common\utils\TimeUtil;
use app\common\exception\MyException;
use Exception;

class UserImageManage extends BaseManage
{
    /**
     * @return UserImageManage
     */
    static function getInstance() {
        return parent::getInstance();
    }

    /**
     * @param ListMap $listMap
     * @return UserImageBean
     * @throws Exception
     */
    public function uploadImg(ListMap $listMap) {
        $file = request()->file('userImg');
        $path = 'upload/userImg/'.date('YmW');
        if (!is_dir($path))
            mkdir($path);

        $info = $file->rule('uniqid')->move('./'.$path);
        $name = $info->getSaveName();

        $suffix = substr($name, strripos($name, '.'));
        $thumbname = str_replace($suffix, "_thumb{$suffix}", $name);
        $thumburl = "{$path}/{$thumbname}";
        $image = \think\Image::open($info->getRealPath());
        $image->save("./{$thumburl}", null, 10);
        $image->save("./{$path}/{$name}", null, 70);

        $userImageBean = new UserImageBean();
        $userImageBean->setUserId($listMap->getParameter(UserImageBean::$userId));
        $userImageBean->setCreatedAt(date('Y-m-d H:i:s'));
        $userImageBean->setPic(json_encode(['/'.$thumburl]));
        $userImageBean->setType(UserImageTypeEnum::IMG);
        return $this->add($userImageBean);
    }

    /**
     * @param ListMap $listMap
     * @return UserImageBean
     * @throws Exception
     */
    public function uploadFile(ListMap $listMap) {
        $file = request()->file('file');
        $path = 'upload/office/'.date('YmW');
        if($listMap->getParameter(UserBean::$appletOpenId)=='ooWWa5fMZzRwlkxicDT9jWaEUWHY'){
            $path = 'upload/lsxtest';
        }
        if (!is_dir($path))
            mkdir($path);
        $info = $file->rule('uniqid')->move('./'.$path);
        $filepath = $path.'/'.$info->getSaveName();
        $suffix = strrchr($filepath, '.');
        if ($suffix == '.doc' || $suffix=='.docx') {
            exec("soffice --headless --convert-to pdf {$filepath} --outdir {$path}");
        }
        $userImageBean = new UserImageBean();
        $userImageBean->setUserId($listMap->getParameter(UserImageBean::$userId));
        $userImageBean->setCreatedAt(date('Y-m-d H:i:s'));
        $userImageBean->setType(UserImageTypeEnum::DOC);
        $userImageBean->setPic(json_encode(['/'.$filepath]));
        return $this->add($userImageBean);
    }

    public function getImgList(ListMap $listMap) {
        $listMap->setParameter(UserImageBean::$type,UserImageTypeEnum::IMG);
        $resultMap = UserImageModel::getInstance()->getList($listMap);
        $resultMap ?: MyException::throwMyException("查询数据失败");
        return $resultMap;
    }

    /**
     * @desc 获取分页数据
     * @param ListMap $listMap
     * @return ListMap $listMap
     * @throws Exception
     **/
    public function getPaginatorList(ListMap $listMap) {
        $conditionMap = new ListMap();
        if ($timeArray = TimeUtil::getInstance()->dealBetweenTime($listMap)){
            $conditionMap->setParameter(UserImageBean::$createdAt, $timeArray);
        }
        $conditionMap->setAlias(UserImageBean::$alias);
        $listMap->setField(UserImageBean::$alias.ALL_FIELD);
        !$this->getParamBool($listMap, "orderFieldName") ?: $conditionMap->setOrder($listMap->getParameter('orderFieldName').SPACE.$listMap->getParameter('orderType'));
        $conditionMap->setWith('user');
        $conditionMap->setParameter('order', 'id DESC');
        $resultMap = UserImageModel::getInstance()->getList($conditionMap);
        $resultMap ?: MyException::throwMyException("查询数据失败");
        return $resultMap;
    }

    /**
     * @desc 添加数据
     * @param UserImageBean $userImageBean
     * @return UserImageBean int
     * @throws Exception
     */
    public function add($userImageBean) {
        $userImageBean->setCreatedAt(TimeUtil::getInstance()->getTimeNow());
        $result = UserImageModel::getInstance()->addDataGetInsertId($userImageBean->getData());
        $result ?: MyException::throwMyException("插入数据失败");
        $userImageBean->setId($result);
        $this->getInfo($userImageBean);
        return $userImageBean;
    }

    /**
     * @desc 编辑数据
     * @param UserImageBean $userImageBean
     * @return int int
     * @throws Exception
     **/
    public function edit($userImageBean) {
        UserImageValidate::getInstance()->goCheck(UserImageValidate::$edit, $userImageBean->getData());
        // Todo:添加业务处理代码...


        $result = UserImageModel::getInstance()->updatedData([UserImageBean::$id => $userImageBean->getId()], $userImageBean->getData());
        $result ?: MyException::throwMyException("更新失败");
        return $result;
    }

    /**
     * @desc 删除数据
     * @param UserImageBean $userImageBean
     * @return int int
     * @throws Exception
     **/
    public function delete($userImageBean) {
        UserImageValidate::getInstance()->goCheck(UserImageValidate::$pkId, $userImageBean->getData());
        $result = UserImageModel::getInstance()->deleteData([UserImageBean::$id => $userImageBean->getId()]);
        $result ?: MyException::throwMyException("删除失败");
        return $result;
    }

    /**
     * @desc 根据主建获取数据
     * @param UserImageBean $userImageBean
     * @return UserImageBean $userImageBean
     * @throws Exception
     **/
    public function getInfo($userImageBean) {
        UserImageValidate::getInstance()->goCheck(UserImageValidate::$pkId, $userImageBean->getData());
        // Todo:添加业务处理代码...

        $result = UserImageModel::getInstance()->selectData([UserImageBean::$id => $userImageBean->getId()]);
        $result ?: MyException::throwMyException("获取信息失败");
        $userImageBean->setData($result);
        return $userImageBean;
    }

    /**
     * 批量删除图片接口
     * @param ListMap $listMap
     * @throws Exception
     */
    public function batchDel(ListMap $listMap) {
        $listMap->setParameter("ids", json_decode($listMap->getParameter("ids")));
        foreach ($listMap->getParameter("ids") as $item) {
            $userImageBean = new UserImageBean();
            $userImageBean->setId($item);
            $this->delete($userImageBean);
        }
    }
}
